package cn.iocoder.yudao.module.book.service.WeChat;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSchemeService;
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateSchemeRequest;
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateSchemeRequest.JumpWxa;
import org.springframework.stereotype.Service;

@Service
public class SchemeService {

    private final WxMaService wxMaService;

    public SchemeService(WxMaService wxMaService) {
        this.wxMaService = wxMaService;
    }

    /**
     * 获取小程序 Scheme 链接
     * @param path 小程序路径，例如 "pages/index/index"
     * @param query 路径参数，例如 "id=123"
     * @param expireTime 过期时间（秒），null 表示永久有效
     * @return Scheme 链接
     */
    public String getWxMaScheme(String path, String query, Long expireTime) {
        try {
            WxMaGenerateSchemeRequest request = WxMaGenerateSchemeRequest.newBuilder()
                    .jumpWxa(WxMaGenerateSchemeRequest.JumpWxa.newBuilder()
                            .path(path)
                            .query(query)
                            .build())
                    .expireTime(expireTime)
                    .build();
            return wxMaService.getWxMaSchemeService().generate(request);
        } catch (Exception e) {
            throw new RuntimeException("生成小程序Scheme链接失败", e);
        }
    }
}
